<?php
//Created by Huynh.
//Last modified by Huynh
//On Feb 28th, 2012


//TODO: 
// - send the card dealing info to players.
// - Calculate and update players' account amounts 
// according to the result of the game.


//NOTE: In  this file, I assume that the possible values of 
//a card are 1, 2.. or 9. Change this according to the rule of 
//the game.
define("MAX_VALUE", 9);

include_once("card.php");
include_once("deck.php");

//This number will be sent from ...
$numberOfPlayers = 4; //I'll return to this later.
$deck = array(); //Array of card deck.
$result = array(); //This array will store the result of a player compared to the host.
$hostNumber = 1; //The host's number.

//Ensure that random numbers are indeed random.
function make_seed()
{
	list($usec, $sec) = explode('.', microtime());
	return (float)$sec + ((float)$usec * 100000);
}//End function make seed.

srand(make_seed());
$arr = array(); //The array of card.
for ($i = 0; $i < 3 * $numberOfPlayers; $i++)
{
	//generate a card.
	while (true)
	{
		//get the card's value.
		$x = rand(1, MAX_VALUE);
		
		//get the card's suit.
		$y = rand(1, 4);
	
		$card = new Card($x, $y);
		
		//Make sure that this card differs from previous card.
		$continue = false;
		
		//Compare this card to the previous cards.
		//If this one is the same as a card generated before, we have to
		//regenerate it.
		for ($j = 0; $j < $i; $j++)
		{
			if ($card->compareTo($arr[$j]) == 0) //regenerate it.
			{
				$continue = true;
			}
		}
		
		if ($continue == false)
		{
			break;
		}
	}
	$arr[$i] = $card;
}


//Generate numberOfPlayer decks.
for ($i = 0; $i < $numberOfPlayers; $i++)
{
	$deck[$i] = new Deck($arr[$i * 3], $arr[$i * 3 + 1], $arr[$i * 3 + 2]);
	
	//Print the deck.
	$deck[$i]->printDeck();
	print "---------------</br>";
	print "Total value: " .$deck[$i]->getValue() ."<br/><br/>" ;
}

//Compare the valud of every deck to the host's deck.
//If greater -> 1, if smaller ->-1.
print "Final result:<br/>**********************<br/>";
for ($i = 0; $i < $numberOfPlayers; $i++)
{
	$result[$i] = $deck[$i]->compareTo($deck[$hostNumber]);
	print $result[$i]."<br/>";
}
?>